import { normalResponse } from './normalResponse'
import { repeatResponseSuccess, repeatResponseError } from './repeatResponse'
import type { AxiosError, AxiosResponse } from 'axios'

const repeatError = new Set()

function errorCreate(message: string) {
  //合并重复错误消息
  if (!repeatError.has(message)) {
    repeatError.add(message)
    // ElMessage({
    //   message,
    //   type: 'error',
    //   onClose: () => {
    //     repeatError.delete(message)
    //   }
    // })
  }
  throw new Error(message)
}

export const responseInterceptors = async (response: AxiosResponse) => {
  try {
    repeatResponseSuccess(response)
    return await normalResponse(response)
  } catch (err) {
    errorCreate('系统繁忙')
    return Promise.reject(new Error('系统繁忙'))
  }
}

export const errorResponseInterceptors = async (error: AxiosError) => {
  try {
    return await repeatResponseError(error)
  } catch (err) {
    handleErrorCatch(err)
  }
}

export const handleErrorCatch = (error: any) => {
  const status = error.response?.status
  switch (status) {
    case 400:
      errorCreate('参数错误')
      break
  }
}
